package com.qiaomeng.flutter.flutter_album_save;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import io.flutter.plugin.common.MethodChannel;

public class SaveHandler {
    private Context context;
    private MediaScannerConnection mMediaonnection;

    public SaveHandler(Context context) {
        this.context = context;
    }

    public void saveImageToAlbum(String imagePath, MethodChannel.Result result) {
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
        saveImageToGallery(bitmap);
        Map map = new HashMap();
        map.put("status", true);
        map.put("msg", "保存成功");
        result.success(map);
    }
    /**
     * 图片保存相册方法
     *
     * @param bitmap
     */

    public void saveImageToGallery(Bitmap bitmap) {
        // 首先保存图片
        File file = null;
        String fileName = System.currentTimeMillis() + ".jpg";
        File root = new File(context.getExternalCacheDir(), context.getPackageName());
        if (!root.exists()) {
            root.mkdir();
        }
        file = new File(root, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        //其次把文件插入到系统图库
        try {
            String insertImage = MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
            File file1 = new File(getRealPathFromURI(Uri.parse(insertImage),context));
            updatePhotoMedia(file1,context);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
    //更新图库
    private  void updatePhotoMedia(File file ,Context context){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(file));
        context.sendBroadcast(intent);
    }
    //得到绝对地址
    private static String getRealPathFromURI(Uri contentUri,Context context) {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String fileStr = cursor.getString(column_index);
        cursor.close();
        return fileStr;
    }

}
